/ leetcodesolve / Longest Palindromic Substring.py
Swadhin Biswas Add files via upload · 4/18/2024
aca18cf
29 lines · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
    def longestPalindrome(self, s: str) -> str:
        if s[::-1] == s:
            return s
        
        # inti start at 0 and window size is 1
        start, size = 0, 1

        for i in range(1, len(s)):
            # set up l, r pointer , base on cur idx to left
            # cur window size = max window size + 1
            l, r = i - size, i + 1

            # cur window size = max window size + 2
            s1 = s[l-1:r]

            # if s1 is palindrome, update start idx and the size
            if l >= 1 and s1[::-1] == s1:
                size += 2
                start = l - 1
            else:
                # cur window size = max window size + 1
                s2 = s[l:r]
                if s2[::-1] == s2:
                    size += 1
                    start = l
        
        return s[start:start+size]